<template>
  <div class="app-container">
    <h2 style="text-align: center;">发布新课程</h2>

    <el-steps
      :active="2"
      process-status="wait"
      align-center
      style="margin-bottom: 40px;">
      <el-step title="填写课程基本信息"/>
      <el-step title="创建课程大纲"/>
      <el-step title="提交审核"/>
    </el-steps>

    <el-button type="text" @click="showChapterDialog">添加章节</el-button>
    <!-- 章节 -->
    <ul class="chapternice">
      <li v-for="chapter in chapterVOList">
        <p>{{ chapter.title }}
          <span class="acts">
        <el-button type="text">添加视频</el-button>
        <el-button style type="text">编辑</el-button>
        <el-button type="text">删除</el-button>
      </span>
        </p>
        <!-- 视频 -->
        <ul class="chapternice videoList">
          <li v-for="video in chapter.videoVOList">
            <p>{{ video.title }}
              <span class="acts">
            <el-button type="text">编辑</el-button>
            <el-button type="text">删除</el-button>
          </span>
            </p>
          </li>
        </ul>
      </li>
    </ul>

    <el-form label-width="120px">
      <el-form-item>
        <el-button @click="previous">上一步</el-button>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="next">下一步</el-button>
      </el-form-item>
    </el-form>

    <!-- Dialog窗口: 添加和修改章节表单 -->
    <el-dialog :visible.sync="dialogChapterFormVisible" title="添加章节">
      <el-form :model="chapter" label-width="120px">
        <el-form-item label="章节标题">
          <el-input v-model="chapter.title"/>
        </el-form-item>
        <el-form-item label="章节排序">
          <el-input-number v-model="chapter.sort" :min="0" controls-position="right"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogChapterFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleChapterSubmit">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>

import {getChapterAndVideo, addChapter} from "@/api/course/chapter";

export default {
  data() {
    return {
      saveBtnDisabled: false,
      courseId: undefined,
      chapterVOList: [],
      dialogChapterFormVisible: false,
      chapter: {}
    }
  },
  methods: {
    previous() {
      // 跳转到add.vue页面，需要将courseID传送到add.vue页面
      this.$router.push({path: "/course/course/add/", query: {id: this.courseId}});
    },
    next() {
      // 跳转到课程发布页面，课程ID通过路由传参
      this.$router.push({path: "/course/course/publish", query: {id: this.courseId}})
    },
    getList() {
      getChapterAndVideo(this.courseId).then(response => {
        // 响应的数据赋值给相关的数据模型
        this.chapterVOList = response.data
      });
    },
    // 添加或者修改章节
    handleChapterSubmit() {
      // 把课程ID设置到章节对象中
      this.chapter.courseId = this.courseId;
      // 判断是新增操作还是修改操作
      if (this.chapter.id == undefined) {// 新增
        addChapter(this.chapter).then(response => {
          // 显示提示信息
          this.$modal.msgSuccess("保存章节信息成功");
          // 隐藏弹窗
          this.dialogChapterFormVisible = false
          // 重新根据课程ID拉取章节及视频数据
          this.getList(this.courseId)
        });
      } else {// 修改操作

      }
    },
    // 显示章节的弹窗
    showChapterDialog() {
      this.dialogChapterFormVisible = true

    }
  },
  created() {
    // 获取add页面路由中传过来的course的ID
    // 获取路由中的参数
    this.courseId = this.$route.query.id

    // 根据课程ID查询章节及视频的相关数据
    this.getList();
  }
}
</script>
<style scoped>
.chapternice {
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
}

.chapternice li {
  position: relative;
}

.chapternice p {
  margin-top: 2px;
  float: left;
  font-size: 16px;
  padding-left: 10px;
  height: 40px;
  line-height: 35px;
  width: 100%;
  border: 1px solid #DDD;
}

.chapternice .acts {
  padding-right: 10px;
  float: right;
  font-size: 10px;
}

.videoList {
  padding-left: 50px;
}

.videoList p {
  margin-top: 2px;
  float: left;
  font-size: 14px;
  height: 35px;
  line-height: 30px;
  width: 100%;
  border: 1px dotted #DDD;
}
</style>
